//
//  StarVideoViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/2/6.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "StarVideoViewController.h"
#import "VideoRecordViewController.h"
#import "StarLivingListCell.h"
#import "FlowLayout.h"
#import "VideoListModel.h"

@interface StarVideoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    NSMutableArray *_VideoListArray;
}
@property (nonatomic, strong) UICollectionView *videCollectionView;
@property (nonatomic, strong) FlowLayout *flowLayout;
@end

@implementation StarVideoViewController
static NSString *starlivingListCellID = @"starlivingListCellID";

#pragma mark -- lazy
- (UICollectionView *)livingCollectionView {
    if (!_videCollectionView) {
        self.flowLayout = [[FlowLayout alloc] init];
        [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _videCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT - 64) collectionViewLayout:self.flowLayout];
        self.flowLayout.count = 12;
        [_videCollectionView registerNib:[UINib nibWithNibName:@"StarLivingListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:starlivingListCellID];
        _videCollectionView.delegate = self;
        _videCollectionView.dataSource = self;
        [self.view addSubview:_videCollectionView];
    }
    return _videCollectionView;
}


#pragma mark -- 回放列表请求
- (void)videoRecordRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
//                           @"page":@"",
//                           @"amount":@""
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kVideoList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *videoDict in array) {
                VideoListModel *model = [[VideoListModel alloc] initWithDict:videoDict];
                [_VideoListArray addObject:model];
            }
            [self.videCollectionView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self videoRecordRequest];
    
    [self setUpUI];
    
    [self setNav];
}

- (void)setUpUI {
    _VideoListArray = [NSMutableArray arrayWithCapacity:1];
    self.livingCollectionView.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setNav {
    self.navigationItem.title = @"更多回放";
}

#pragma mark -- DataSource Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _VideoListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StarLivingListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:starlivingListCellID forIndexPath:indexPath];
    VideoListModel *model = _VideoListArray[indexPath.row];
    cell.videolistModel = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
     VideoListModel *model = _VideoListArray[indexPath.row];
    VideoRecordViewController *recordVC = [[VideoRecordViewController alloc] init];
    recordVC.channelId = model.l_channel;
    [self.navigationController pushViewController:recordVC animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kSCREEN_WIDTH - 40)/2, (kSCREEN_WIDTH - 40)/2 + 30);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 0, 15);//分别为上、左、下、右
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
